/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Control;
import Model.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Administrator
 */
public class AirportServiceI implements AirportService {
    private AirportI ci;
    private AirportConnection conn = new AirportConnection();
    private Statement st = null;
    private ResultSet rs = null;

    public AirportI findByAirportName(AirportI ci) {
        this.ci = new AirportI();
        this.dbConnection();
        this.getResultSet("SELECT * FROM airport WHERE airportName='" + ci.getAirportName() + "'");
        try {
            this.ci.setAirportName(rs.getString("airportName"));
            this.ci.setAirportTax(rs.getString("airportTax"));
            this.ci.setAirportGSD(rs.getString("airportGSD"));
            this.ci.setAirportTel(rs.getString("airportTel"));
            return this.ci;
        } catch (SQLException ex) {
            this.ci = null;
            System.out.println("Method AirportServiceI.findByAirportName() failure!");
            ex.printStackTrace();
            return this.ci;
        }

    }

    public void insertAirport(AirportI ci) {
        this.dbConnection();
        this.exeInsert("Insert Into airport(airport_Name,airport_Tax,airport_GSD,airport_Tel) Values("+"'" + ci.getAirportName() + "','" + ci.getAirportTax() + "','" + ci.getAirportGSD() + "','" + ci.getAirportTel()+ "')");
    }

    public void dbConnection() {
        try {
            conn.Load_DB();
            conn.Connection_DB();
        } catch (ClassNotFoundException ex) {
            System.out.println("Class Not Found!");
            ex.printStackTrace();
        } catch (SQLException sqle) {
            System.out.println("Connection Dababase failure!");
            sqle.printStackTrace();
        }
    }

    public void getResultSet(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    rs = st.executeQuery(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method AirportServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public void exeInsert(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method AirportServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public List findAll() {
        AirportI cu;
        ArrayList list = new ArrayList();
        this.dbConnection();
        this.getResultSet("SELECT * FROM airport");
        try {
            while (rs.next()) {
                cu = new AirportI();
                cu.setAirportName(rs.getString("airport_Name"));
                cu.setAirportTax(rs.getString("airport_Tax"));
                cu.setAirportGSD(rs.getString("airport_GSD"));
                cu.setAirportTel(rs.getString("airport_Tel"));
                list.add(cu);
            }
        } catch (SQLException ex) {
            System.out.println("Method AirportServiceI.findAll() failure!");
            ex.printStackTrace();
        }
        return list;
    }

    public void deleteAirport(AirportI ci) {
        this.dbConnection();
        this.exeDelete("Delete From airport Where airportName='" + ci.getAirportName()+"'");
    }

    public void exeDelete(String sql){
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method AirportServiceI.exeDelete() failure!");
            sqle.printStackTrace();
        }
    }
}
